package gridmapper;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.util.ResourceBundle;
import javax.swing.AbstractButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

/**
 *
 * @author estell
 */
public class MasterFrame extends javax.swing.JFrame {

	private String mapFileName = "xml/testmap.xml";

	public static void main(String[] args) {
		new MasterFrame();
	}

	public MasterFrame() {
		initComponents();
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		mapPanel1.buildMap(mapFileName);
		mapPanel1.setTileType("Building");

		masterImage1.loadImage("resources/images/tiles2.png");

		Dimension dim = new Dimension(masterImage1.getImag().getWidth(), masterImage1.getImag().getHeight());
		masterImage1.setPreferredSize(dim);
		masterImage1.setMaximumSize(dim);
		masterImage1.setMinimumSize(dim);
		masterImage1.setMapLink(mapPanel1);
		mapPanel1.setImageLink(masterImage1);

		this.setResizable(true);
		this.setVisible(true);

		this.createBufferStrategy(2);

		gameLoop();
	}

	private void gameLoop() {
		//GNDN
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        scrl_Main = new javax.swing.JScrollPane();
        mapPanel1 = new gridmapper.MapPanel();
        masterImage1 = new gridmapper.MasterImage();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem3 = new javax.swing.JMenuItem();
        mnu_Save = new javax.swing.JMenuItem();
        mnu_New = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        mnucb_ShowGrid = new javax.swing.JCheckBoxMenuItem();
        jSeparator2 = new javax.swing.JPopupMenu.Separator();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        scrl_Main.setPreferredSize(new java.awt.Dimension(500, 500));

        javax.swing.GroupLayout mapPanel1Layout = new javax.swing.GroupLayout(mapPanel1);
        mapPanel1.setLayout(mapPanel1Layout);
        mapPanel1Layout.setHorizontalGroup(
            mapPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 707, Short.MAX_VALUE)
        );
        mapPanel1Layout.setVerticalGroup(
            mapPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 728, Short.MAX_VALUE)
        );

        scrl_Main.setViewportView(mapPanel1);

        jSplitPane1.setLeftComponent(scrl_Main);

        masterImage1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        masterImage1.setFocusable(false);
        masterImage1.setPreferredSize(new java.awt.Dimension(300, 250));

        javax.swing.GroupLayout masterImage1Layout = new javax.swing.GroupLayout(masterImage1);
        masterImage1.setLayout(masterImage1Layout);
        masterImage1Layout.setHorizontalGroup(
            masterImage1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 296, Short.MAX_VALUE)
        );
        masterImage1Layout.setVerticalGroup(
            masterImage1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 496, Short.MAX_VALUE)
        );

        jSplitPane1.setRightComponent(masterImage1);

        jMenu1.setText("File");

        jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem3.setText("Open Map");
        jMenu1.add(jMenuItem3);

        mnu_Save.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        mnu_Save.setText("Save Map");
        mnu_Save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnu_SaveActionPerformed(evt);
            }
        });
        jMenu1.add(mnu_Save);

        mnu_New.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        mnu_New.setText("New Map");
        mnu_New.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnu_NewActionPerformed(evt);
            }
        });
        jMenu1.add(mnu_New);

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem1.setText("Print Map");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);
        jMenu1.add(jSeparator1);

        mnucb_ShowGrid.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_G, java.awt.event.InputEvent.ALT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        mnucb_ShowGrid.setText("Show Grid");
        mnucb_ShowGrid.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnucb_ShowGridActionPerformed(evt);
            }
        });
        jMenu1.add(mnucb_ShowGrid);
        jMenu1.add(jSeparator2);

        jMenuItem4.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem4.setText("Exit");
        jMenu1.add(jMenuItem4);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");

        jMenuItem2.setText("Modify Map Size");
        jMenu2.add(jMenuItem2);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jSplitPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jSplitPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void mnu_NewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnu_NewActionPerformed
		//System.out.println("User performed New Action");
		//NewMapOptions dialog = new NewMapOptions(this, false);
		//dialog.setVisible(true);

		String input = JOptionPane.showInputDialog("Enter Input (width,height):");
		CharSequence seq = ",";
		if (input != null && !input.equals("") && input.contains(seq)) {
			System.out.println(input);
			String[] parsedVals = input.split(",");
			mapPanel1.buildNewMap(Integer.parseInt(parsedVals[0]), Integer.parseInt(parsedVals[1]));
			mapPanel1.repaint();
			scrl_Main.updateUI();
			scrl_Main.validate();
			scrl_Main.repaint();

		} else {
			JOptionPane.showMessageDialog(this, "ERROR", "ERROR TITLE", JOptionPane.ERROR_MESSAGE);
		}

	}//GEN-LAST:event_mnu_NewActionPerformed

	private void mnu_SaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnu_SaveActionPerformed
		System.out.println("User performed Save Action");
	}//GEN-LAST:event_mnu_SaveActionPerformed

	private void mnucb_ShowGridActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnucb_ShowGridActionPerformed
		System.out.println("Grid Toggled");

		AbstractButton aButton = (AbstractButton) evt.getSource();
		boolean selected = aButton.getModel().isSelected();
		if (selected) {
			mapPanel1.setShowGrid(true);
		} else {
			mapPanel1.setShowGrid(false);
		}
		mapPanel1.repaint();
		scrl_Main.updateUI();
		scrl_Main.validate();
		scrl_Main.repaint();
	}//GEN-LAST:event_mnucb_ShowGridActionPerformed

	private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
		System.out.println("User selected Print" +"");
		try {
			printComponent();
		} catch (Exception prt) {
			System.err.println(prt.getMessage());
		}
	}

	public void printComponent(){

	  PrinterJob pj = PrinterJob.getPrinterJob();
	  pj.setJobName(" Print Component ");

	  	if (pj.printDialog()) {
		  pj.setPrintable (new Printable() {
			public int print(Graphics pg, PageFormat pf, int pageNum){
			  if (pageNum > 0){
				return Printable.NO_SUCH_PAGE;
			  }

			  Graphics2D g2 = (Graphics2D) pg;
			  g2.translate(pf.getImageableX(), pf.getImageableY());
			  mapPanel1.paint(g2);
			  return Printable.PAGE_EXISTS;
			}
		  });
		}else{
			return;
	  }

	  try {
			pj.print();
	  } catch (PrinterException ex) {
			// handle exception
	  }
	}


	public JMenu getjMenu1() {
		return jMenu1;
	}

	public void setjMenu1(JMenu jMenu1) {
		this.jMenu1 = jMenu1;
	}

	public JMenu getjMenu2() {
		return jMenu2;
	}

	public void setjMenu2(JMenu jMenu2) {
		this.jMenu2 = jMenu2;
	}

	public JMenuBar getjMenuBar1() {
		return jMenuBar1;
	}

	public void setjMenuBar1(JMenuBar jMenuBar1) {
		this.jMenuBar1 = jMenuBar1;
	}

	public JMenuItem getjMenuItem1() {
		return jMenuItem1;
	}

	public void setjMenuItem1(JMenuItem jMenuItem1) {
		this.jMenuItem1 = jMenuItem1;
	}

	public JSplitPane getjSplitPane1() {
		return jSplitPane1;
	}

	public void setjSplitPane1(JSplitPane jSplitPane1) {
		this.jSplitPane1 = jSplitPane1;
	}

	public String getMapFileName() {
		return mapFileName;
	}

	public void setMapFileName(String mapFileName) {
		this.mapFileName = mapFileName;
	}

	public MapPanel getMapPanel1() {
		return mapPanel1;
	}

	public void setMapPanel1(MapPanel mapPanel1) {
		this.mapPanel1 = mapPanel1;
	}

	public MasterImage getMasterImage1() {
		return masterImage1;
	}

	public void setMasterImage1(MasterImage masterImage1) {
		this.masterImage1 = masterImage1;
	}

	public JMenuItem getMnu_New() {
		return mnu_New;
	}

	public void setMnu_New(JMenuItem mnu_New) {
		this.mnu_New = mnu_New;
	}

	public JMenuItem getMnu_Save() {
		return mnu_Save;
	}

	public void setMnu_Save(JMenuItem mnu_Save) {
		this.mnu_Save = mnu_Save;
	}

	public JCheckBoxMenuItem getMnucb_ShowGrid() {
		return mnucb_ShowGrid;
	}

	public void setMnucb_ShowGrid(JCheckBoxMenuItem mnucb_ShowGrid) {
		this.mnucb_ShowGrid = mnucb_ShowGrid;
	}

	public JScrollPane getScrl_Main() {
		return scrl_Main;
	}

	public void setScrl_Main(JScrollPane scrl_Main) {
		this.scrl_Main = scrl_Main;
	}//GEN-LAST:event_jMenuItem1ActionPerformed



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JPopupMenu.Separator jSeparator2;
    private javax.swing.JSplitPane jSplitPane1;
    private gridmapper.MapPanel mapPanel1;
    private gridmapper.MasterImage masterImage1;
    private javax.swing.JMenuItem mnu_New;
    private javax.swing.JMenuItem mnu_Save;
    private javax.swing.JCheckBoxMenuItem mnucb_ShowGrid;
    private javax.swing.JScrollPane scrl_Main;
    // End of variables declaration//GEN-END:variables
}
